home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Collection of Tools & Utilities
/
Collection of Tools and Utilities.iso
/
edit
/
tde40.zip
/
tdecfg.c
< prev
next >
Wrap
C/C++ Source or Header
|
1994-06-05
|
28KB
|
894 lines
/*
* A configuration utility was written to customize the tde executable file.
* You only need one file to run tde - the executable. No configuration files
* to worry about.
*
* With this version, there is no need to figure the offsets each time
* tde.exe is modified. The original program to find the offsets was written
* by Jim Lee, jlee@ece.orst.edu. Simple pattern matching machines are used
* to find the offsets. On one pass thru tde.exe, the machines will quickly
* find all signatures.
*
* ------------------------------------------------------------------------
* Jim Lee, jlee@ece.orst.edu, sent working code for finding the offsets of
* of various data structures in tde.exe. Hoping to improve on the brute
* force method, I tried using a more complicated algorithm. Needless to
* say and standard operating procedure, I did not correctly implement
* the more complicated algorithm. For example,
*
* my implementation would not find "$ help" in the string "$$ help"
*
* All bugs in Jim Lee's working code were introduced by me, Frank.
*
* Jimmy Thompson, jimmy_t@verifone.com, found the error and provided
* working code for the Knuth-Morris-Pratt algorithm. Jimmy, thank you
* for the error report and the solution.
*
* That's what I get for acting like I'm so smart. Not only did I
* incorrectly implement the Aho-Corasick algorithm, but I should have
* used the Knuth-Morris-Pratt algorithm.
*
* Frank Davis, August, 29, 1993, TDE 3.1
* ------------------------------------------------------------------------
*
* See:
*
* Donald Ervin Knuth, James H. Morris, Jr., and Vaughan R. Pratt,
* "Fast Pattern Matching in Strings." _SIAM Journal on Computing_
* 6 (No. 2): 323-350, 1977.
*
* Robert Sedgewick, _Algorithms in C_, Addison-Wesley, Reading, Mass.,
* 1990, Chapter 19, "String Searching", pp. 277-292, ISBN 0-201-51425-7
*
*
* Knuth-Morris-Pratt in TDE
*
* To be safe and sure, the KMP algorithm is pretty much based on the
* description given in _SIAM J on Comp_. Hints for KMP in C are from
* Dr. Sedgewick's book. Actually, on average, finding a *text* string with
* KMP is not that much faster, if any, than a brute force search. However,
* using KMP for finding several strings at once is probably faster than
* a brute force implementation. Anyway, I think I got the implementation
* right, this time...
*
*
* Program name: tdecfg
* Author: Frank Davis
* Date: October 5, 1991
* Date: June 5, 1993
*
* This program is released into the public domain. You may distribute
* it freely, Frank Davis.
*/
#include <bios.h>
#include <dos.h>
#include <io.h>
#include <malloc.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "tdecfg.h"
struct vcfg cfg; /* video stuff */
FILE *tde_exe; /* FILE pointer to tde.exe */
long sort_offset;
long mode_offset;
long color_offset;
long macro_offset;
long keys_offset;
long two_key_offset;
long help_offset;
struct screen cfg_choice[] = {
{5,25,"1. Change colors" },
{7,25,"2. Redefine keys" },
{9,25,"3. Install new help screen" },
{11,25,"4. Set default modes" },
{13,25,"5. Install permanent macro file" },
{15,25,"6. Read in a configuration file" },
{17,25,"7. Exit" },
{20,20,"Please enter choice: " },
{0,0,NULL}
};
char *greatest_composer_ever = "W. A. Mozart, 1756-1791";
/*
* Name: main
* Date: October 5, 1991
* Notes: Strategy is fairly straight forward - 1) initialize all the
* variables 2) show the user a color sample 3) make the changes
* permanent if desired.
*/
void main( int argc, char *argv[] )
{
int rc;
int c;
char fname[82];
char *buff;
/*
* lets get a 8k buffer for our pattern matching machines.
*/
if ((buff = malloc( 8200 )) == NULL) {
puts( "\nNot enough memory." );
exit( 1 );
}
puts( "\nEnter tde executable file name (<Enter> = \"tde.exe\") :" );
gets( fname );
if (strlen(fname) == 0)
strcpy( fname, "tde.exe" );
if ((rc = access( fname, EXIST )) != 0) {
puts( "\nFile not found." );
exit( 1 );
} else if ((tde_exe = fopen( fname, "r+b" )) == NULL ) {
puts( "\nCannot open executable file." );
exit( 2 );
}
if ((rc = find_offsets( buff )) == ERROR)
puts( "\nFatal error finding offsets.\n" );
free( buff );
if (rc == ERROR)
exit( 3 );
video_config( );
cls( );
show_box( 0, 0, cfg_choice, NORMAL );
for (rc=0; rc != 1;) {
xygoto( 42, 20 );
c = getkey( );
while (c != '1' && c != '2' && c != '3' && c != '4' && c != '5' &&
c != '6' && c != '7')
c = getkey( );
switch (c) {
case '1' :
tdecolor( );
show_box( 0, 0, cfg_choice, NORMAL );
break;
case '2' :
tdekeys( );
show_box( 0, 0, cfg_choice, NORMAL );
break;
case '3' :
tdehelp( );
show_box( 0, 0, cfg_choice, NORMAL );
break;
case '4' :
tdemodes( );
show_box( 0, 0, cfg_choice, NORMAL );
break;
case '5' :
tdemacro( );
show_box( 0, 0, cfg_choice, NORMAL );
break;
case '6' :
tdecfgfile( );
show_box( 0, 0, cfg_choice, NORMAL );
break;
case '7' :
rc = 1;
break;
}
}
fcloseall( );
puts( " " );
puts( " " );
}
/*********************** original comments *************************/
/*
** OFFSETS.C - Automatically scan tde.exe for config offsets
**
** Author: Jim Lee (jlee@ece.orst.edu)
** Date: 5/12/93
** Status: Released to the public domain
**
** This little utility takes the drudgery out of updating tdecfg.h
** every time you re-compile tde.exe. Just remove the hard-coded
** offsets in tdecfg.h and replace them with '#include "newoff.h"'.
** Then run 'offsets tde.exe > newoff.h'. Now re-compile tdecfg
** and you're done!
**
*/
/***************************** end *******************************/
/*
* Name: build_next_table
* Date: August 29, 1993
* Passed: pattern: pattern to look for
* next: array for failure links
* m: length of pattern.
* Notes: the algorithm for building the next table is based the description
* by KMP in _SIAM J Comp_, page 328. Hints for a C implementation
* are from Dr. Sedgewick's book, page 283.
*
* don't make the mistake I did. the first character in the
* sort signature is '\0'. using strlen( ) to get the pattern
* length don't work too good for patterns with '\0' in them.
*/
void build_next_table( char *pattern, char *next, int m )
{
int j;
int t;
int len;
len = m;
t = next[0] = -1;
j = 0;
while (j < len) {
while (t >= 0 && pattern[j] != pattern[t])
t = (int)next[t];
j++;
t++;
next[j] = pattern[j] == pattern[t] ? next[t] : (char)t;
}
}
/*
* Name: find_offsets
* Date: June 5, 1993
* August 29, 1993
* Notes: to increase the speed, we use a pattern matching machine
* for each of the signatures. on one pass through the file, all 7
* signatures will be found. it's also a little faster if we read
* the file in big chunks. also note that we never have to back-up
* or reread the file.
*
* this implementation is pretty much based on the description by
* KMP in _SIAM J Comp_, page 326 (middle of the page.) hints for
* a C implementation are from Dr. Sedgewick's book, page 282, 285.
*
* original KMP implementation by: James H. Thompson, jimmy_t@verifone